import Vue from 'vue'
import Vuex from 'vuex'
import { userApi } from '@/api'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    username: localStorage.getItem('username') || '',
    password: localStorage.getItem('password') || '',
    id: localStorage.getItem('id') || 'AREA|dbf46d32-7e76-1196',
    list: []
  },
  mutations: {
    saveUsername (state, username) {
      localStorage.setItem('username', username)
    },
    savePassword (state, password) {
      localStorage.setItem('password', password)
    },
    saveId (state, id) {
      localStorage.setItem('id', id)
    }
  },
  actions: {
    async getUser (store, user) {
      const res = await userApi(user)
      console.log(res)
      if (res.data.status === 200) {
        store.commit('saveUsername', user.username)
        store.commit('savePassword', user.password)
        return Promise.resolve(res)
      } else {
        return Promise.reject(res.data)
      }
    }
  },
  modules: {

  }
})
